<script setup lang="ts">
import useLoginStore from '@/store/modules/login'
import { firstMenu, mapPathToMenu } from '@/utils/map-menus';

defineProps({
  isFold: {
    type: Boolean,
    default: false
  }
})
// 获取动态菜单
const loginStore = useLoginStore()
const userMenus = loginStore.userMenus
// console.log(userMenus)
const router = useRouter()
const route = useRoute()
const handleMenuClick = (item: any) => {
  const url = item.url
  router.push(url)
}
// 用户进入main页面的默认菜单
const defaultAction = computed(() => {
  const pathMenu = mapPathToMenu(route.path, userMenus)
  return pathMenu.id + ''
})
// console.log(route.path)
</script>

<template>
  <div class="main-menu">
    <div class="logo">
      <img src="@/assets/img/logo.svg" alt="" />
      <h2 v-show="!isFold" class="title">后台管理系统</h2>
    </div>
    <div class="menu">
      <el-menu
        :collapse="isFold"
        :default-active="defaultAction"
        text-color="#b7bdc3"
        active-text-color="#fff"
        background-color="#001529"
      >
        <template v-for="item in userMenus" :key="item.id">
          <el-sub-menu :index="item.id + ''">
            <template #title>
              <!-- 使用动态组件component将从服务器获取到的icon图标：el-icon-monitor转换为element-plus图标 -->
              <el-icon>
                <component :is="item.icon.split('el-icon-')[1]"></component>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <template v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item :index="subitem.id + ''" @click="handleMenuClick(subitem)">{{ subitem.name }}</el-menu-item>
            </template>
          </el-sub-menu>
        </template>
      </el-menu>
    </div>
  </div>
</template>

<style scoped lang="less">
.main-menu {
  height: 100%;
  background-color: #001529;
  .el-menu {
    border-right: none;
  }
  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    align-items: center;
    overflow: hidden;

    img {
      height: 100%;
      margin: 0 10px;
    }
    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }
  .el-menu {
    border-right: none;
    user-select: none;
  }
  .el-sub-menu {
    // background-color: #001529 !important;
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  // ::v-deep .el-submenu__title {
  //   background-color: #001529 !important;
  // }
  .el-menu-item:hover {
    color: #fff !important;
  }
  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.le-menu--collapse) {
  width: 100%;
  height: cale(100% -48px);
}
</style>
